//
//  PKGroupListDetailViewController.m
//  PiankeProject
//
//  Created by lanou on 15/10/13.
//  Copyright (c) 2015年 xiaolizi. All rights reserved.
//

#import "PKGroupListDetailViewController.h"
#import "PKRequestManager.h"
#import "PKGroupDetailModel.h"
#import "PKGroupTopicModel.h"
#import "PKGroupTopicViewCell.h"
#import <UIImageView+WebCache.h>
#import "PKGroupTopicDetailViewController.h"
#import "PKGroupIntroduceViewController.h"
@interface PKGroupListDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong) NSMutableArray *dataArray;
@property(nonatomic,strong) PKGroupDetailModel *detailModel;
@property(nonatomic,strong) UITableView *mainTableView;
// 表头上加的视图
@property(nonatomic,strong) UIView *headView;
@property(nonatomic,strong) UIImageView *headImageView;
@property(nonatomic,strong) UILabel *memberNumLabel;
@property(nonatomic,strong) UILabel *postNumLabel;
@property(nonatomic,strong) UIButton *joinButton;
@end

@implementation PKGroupListDetailViewController

- (NSMutableArray *)dataArray
{
    if (!_dataArray)
    {
        self.dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (PKGroupDetailModel *)detailModel
{
    if (!_detailModel)
    {
        self.detailModel = [[PKGroupDetailModel alloc] init];
    }
    return _detailModel;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self requestData];
    [self configureView];
    
    // 注册cell
    [_mainTableView registerClass:[PKGroupTopicViewCell class] forCellReuseIdentifier:@"groupDetail"];
}

- (void)configureView
{
    _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 65, SCREEN_WIDTH, SCREEN_HEIGHT - 65) style:(UITableViewStyleGrouped)];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    _mainTableView.rowHeight = 210;
    
    _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    _headView.backgroundColor = [UIColor whiteColor];
    _headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150)];
    _headImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *recoginizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView)];
    [_headImageView addGestureRecognizer:recoginizer];
    [_headView addSubview:_headImageView];
    
    UIImageView *jiantouView = [[UIImageView alloc] initWithFrame:CGRectMake(330, 70, 30, 30)];
    jiantouView.image = [UIImage imageNamed:@"youjiantou"];
    [_headImageView addSubview:jiantouView];
    
    _memberNumLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 165, 100, 20)];
    _memberNumLabel.font = [UIFont systemFontOfSize:13];
    [_headView addSubview:_memberNumLabel];
    
    _postNumLabel = [[UILabel alloc] initWithFrame:CGRectMake(150, 165, 100, 20)];
    _postNumLabel.font = [UIFont systemFontOfSize:13];
    [_headView addSubview:_postNumLabel];
    
    _joinButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _joinButton.frame = CGRectMake(300, 165, 50, 18);
    _joinButton.backgroundColor = [UIColor colorWithRed:0.000 green:0.710 blue:0.228 alpha:1.000];
    _joinButton.layer.masksToBounds = YES;
    _joinButton.layer.cornerRadius = 10;
    [_joinButton setTitle:@"加入" forState:(UIControlStateNormal)];
    _joinButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [_headView addSubview:_joinButton];
    
    _mainTableView.tableHeaderView = _headView;
    [self.view addSubview:_mainTableView];
}
// 数据请求
- (void)requestData
{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"client"] = @"1";
    parDic[@"deviceid"] = @"63A94D37-33F9-40FF-9EBB-481182338873";
    parDic[@"groupid"] = self.listModel.groupid;
    parDic[@"auth"] = @"Wc06FCrkoq1DCMVzGMTikDJxQ8bm3Mrm2NpT9qWjwzcWP23tBKQx1c4P0";
    parDic[@"version"] = @"3.0.2";
    [PKRequestManager requestWithType:POST url:@"http://api2.pianke.me/group/group_info" parDic:parDic finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSDictionary *dict = dic[@"data"];
        [self.detailModel setValuesForKeysWithDictionary:dict];
        
        NSArray *arr = dic[@"data"][@"postslist"];
        for (NSDictionary *dict1 in arr)
        {
            PKGroupTopicModel *model = [[PKGroupTopicModel alloc] init];
            [model setValuesForKeysWithDictionary:dict1];
            [self.dataArray addObject:model];
        }
        [_mainTableView reloadData];
        
        [_headImageView sd_setImageWithURL:[NSURL URLWithString:self.detailModel.bannerimg]];
        _memberNumLabel.text = [NSString stringWithFormat:@"成员：%@人",self.detailModel.membernum];
        _postNumLabel.text = [NSString stringWithFormat:@"话题：%@个",self.detailModel.postsnum];
        
    } error:nil];
}

- (void)tapImageView
{
    PKGroupIntroduceViewController *introVC = [[PKGroupIntroduceViewController alloc] init];
    introVC.listModel = self.listModel;
    [self.navigationController pushViewController:introVC animated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PKGroupTopicViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"groupDetail" forIndexPath:indexPath];
    cell.topicModel = _dataArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PKGroupListDetailViewController *detailVC = [[PKGroupListDetailViewController alloc] init];
//    detailVC.detailModel = _dataArray[indexPath.row];
    [self.navigationController pushViewController:detailVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
